package com.hd.pzhwlxt.controller.model1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.RouteTbl;
import com.hd.pzhwlxt.service.RouteTblService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-26 19:42
 * @author: 怀念天空
 * @Description:
 */
@RestController
public class RouteController {

    @Autowired
    private RouteTblService routeTblService;

    @GetMapping("/route")
    public Response<Object> getAllRouteInfo(@RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            List<RouteTbl> result = routeTblService.list();
            if (result != null) {
                return Response.createSuccessResponse("查询成功", result);
            }
        } else {
            Page<RouteTbl> result = routeTblService.page(new Page<>(pageNum, pageSize));
            if (result != null) {
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @DeleteMapping("/route/{ids}")
    public Response<String> deleteRouteInfo(@PathVariable Integer[] ids) {
        boolean remove = routeTblService.removeByIds(Arrays.asList(ids));
        if (remove) {
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PostMapping("/route")
    public Response<String> addRouteInfo(@RequestBody RouteTbl routeTbl) {
        // 添加时先计算金额
        boolean save = routeTblService.save(routeTbl);
        if (save) {
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @PutMapping("/route")
    public Response<String> updatePurchaseInfo(@RequestBody RouteTbl routeTbl) {
        // 修改时添加库存信息
        boolean update = routeTblService.updateById(routeTbl);
        if (update) {
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @GetMapping("/route/{id}")
    public Response<Object> getRouteInfo(@PathVariable Integer id) {
        RouteTbl result = routeTblService.getById(id);
        if (result != null) {
            return Response.createSuccessResponse("查询成功", result);
        }
        return Response.createErrorResponse("查询失败");
    }

    //根据起始地和目的地查询路线，同时需要运输类型字段
    @GetMapping("/routes/vehicle")
    public Response<Object> getRouteInfoByVehicle(String startPlace,String endPlace,Integer type){
        QueryWrapper<RouteTbl> wrapper = new QueryWrapper<>();
        wrapper.eq("start_place",startPlace).eq("end_place",endPlace).eq("type",type);
        List<RouteTbl> list = routeTblService.list(wrapper);
        if (list != null){
            return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }

    //查询使用过的铁路专线名称
    @GetMapping("/routes/train")
    public Response<Object> getRouteInfoByTrain(){
        List<RouteTbl> list = routeTblService.trainList();
        if (list != null){
            return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }
}
